package othello.players;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;

public class PlayerResults {
	private List<Double> ratios = new LinkedList<Double>();
	private List<Integer> wins = new LinkedList<Integer>();
	private List<Integer> losses = new LinkedList<Integer>();
	private List<Integer> draws = new LinkedList<Integer>();
	
    private int gamesWon;
    private int gamesLost;
    private int gamesTied;   
    
    private int gamesWonTotal;
    private int gamesLostTotal;
    private int gamesTiedTotal;
    
    public void won() {
		gamesWon++;
		gamesWonTotal++;
	}

	public void lost() {
		gamesLost++;
		gamesLostTotal++;
	}

	public void tied() {
		gamesTied++;
		gamesTiedTotal++;
	}

	public void reset() {
		gamesWon = 0;
		gamesLost = 0;
		gamesTied = 0;
	}

	public void storeRatio() {
		int total = gamesWon + gamesLost + gamesTied;
		//System.out.print("total: " + total + '\n');
    	ratios.add((double)gamesWon / (double)total);
    	wins.add(gamesWon);
    	losses.add(gamesLost);
    	draws.add(gamesTied);
	}
	
	public void saveRatios(String playerName, int run) {
		System.out.println(playerName);
		System.out.println("won:" + this.gamesWonTotal);
		System.out.println("lost:" + this.gamesLostTotal);
		System.out.println("tied:" + this.gamesTiedTotal);
        try {
        	(new File("results")).mkdir();
        	FileWriter fstream = new FileWriter("results/results_" + playerName + '_' + run + ".csv");
        	BufferedWriter out = new BufferedWriter(fstream);        	
        	for(int i = 0; i < wins.size(); ++i) {
        		out.write(String.format("%d", wins.get(i)) + ',' + String.format("%d", draws.get(i)) + ',' + String.format("%d", losses.get(i)) + '\n');
        	}
        	out.close();
        	fstream.close();
        } catch (Exception e){
        	System.err.println("Error: " + e.getMessage());
        }
    }
}
